﻿window.SearchKey = null;
$.MGJ.UserManager = [];
(function () {
    var _this = $.MGJ.UserManager;
    $.extend($.MGJ.UserManager, {
        Execls: function (ele) {
            $("input.AllClickChild:checked").each(function () {
                info.push($(this).attr('ID'));
            });
            if (info.length == 0) {
                $.L.msgError("请选择要导出的经销商！");
                return
            }
            if (!$(ele).find('.no-result').hasClass('no-result')) {
                window.open(ACTION_URL("Execls", "Manager") + "?Key=" + info);
            }
            else {
                $.L.msgWarning('导出数据不能为空！')
            }


        }
    });
})(jQuery);
(function () {
    var _this = $.MGJ.UserManager;
    $.extend($.MGJ.UserManager, {
        // 新增用户
        Dialog_AddUserObj: null,
        Dialog_AddUser: function () {
            this.Dialog_AddUserObj = $.L.dialog({
                url: ACTION_URL("AddUser", "Manager"),
                title: "新增用户",
                width: 900,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            $(".btn-primary").attr("disabled", true);
                            var bootstrapValidator = $("#form_AddUser").data('bootstrapValidator');
                            bootstrapValidator.validate();
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_AddUser();
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                                $(".btn-primary").attr("disabled", false);
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                },
            });
        },
        //编辑
        Dialog_EditUserObj: null,
        Dialog_EditUser: function (obj) {
            var id = $(obj).attr("UserID");
            this.Dialog_EditUserObj = $.L.dialog({
                url: ACTION_URL("EditUser", "Manager") + "?id=" + id,
                title: "编辑信息",
                width: 900,
                height: 'auto',
                async: true,
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                          
                            $(".btn-primary").attr("disabled", true);
                            var bootstrapValidator = $("#form_EditUser").data('bootstrapValidator').validate();
                            
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_EditUserInfo_();
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                                $(".btn-primary").attr("disabled", false);
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },
        //修改密码
        Dialog_ResetCodeObj: null,
        Dialog_ResetCode: function (obj) {
            var id = $(obj).attr("UserID");
            this.Dialog_ResetCodeObj = $.L.dialog({
                url: ACTION_URL("ResetCode", "Manager"),
                title: "重置密码",
                width: 800,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            $(".btn-primary").attr("disabled", true);
                            var bootstrapValidator = $("#form_resetPWD").data('bootstrapValidator').validate();
                            
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_ResetUserPWD(id);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                                $(".btn-primary").attr("disabled", false);
                            };

                            //_this.Ajax_ResetUserPWD(id);
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },



        //修改角色权限
        Dialog_AEditUserObj: null,
        Dialog_AEditUser: function (obj, logEid) {
            AEditUser_id = $(obj).attr("UserID");

            var eid = $(obj).attr("EId");

            this.Dialog_AEditUserObj = $.L.dialog({
                url: ACTION_URL("AEditUser", "Manager") + "?eid=" + eid,
                title: "添加角色",
                width: 900,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
            
                            _this.Ajax_EditUserRole(AEditUser_id, eid);
                      
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },

    });
})(jQuery);


(function () {
    var _index = 1;
    var _this = $.MGJ.UserManager;
    $.extend($.MGJ.UserManager, {
        //绑定 
        Ajax_BinRoleNames: function (text) {
            return text.substring(0, text.length - 1);
        },
        Bin_ViewFlag: function (text) {
            return text == 1 ? "启用" : "禁用";
        },
        //查询用户信息
        Ajax_GetUserInfo: function (pageIndex, SeachStr, Eid) {
            Eid = Eid || 0;
            _index = pageIndex;
            window.SearchKey = removeSpace($("#whereStr").val());
            $.L.ajaxHandle({
                url: ACTION_URL("GetUserInfo", "Manager"),
                data: {
                    pageIndex: pageIndex,
                    pageSize: 10,
                    SeachWhere: window.SearchKey,
                    Eid:Eid
                }, success: function (response) {
                    $("#UserInfo").L_bindingDataList(response.data);
                    $('.EDRoleType').each(function () {
                        if ($(this).attr('type') != 0) {
                            $(this).removeAttr('onclick').css('color', '#C6C7C7');
                        }
                        if ($(this).attr('eName')!=response.eName) {
                            $(this).removeAttr('onclick').css('color', '#C6C7C7');
                        }
                     
                    })
                }
            });
        },
        //多选角色
        Ajax_GetRoleAll: function () {
            $.L.ajaxHandle({
                url: ACTION_URL("GetRoleAll", "Manager"),
                async: false,
                success: function (response) {
                    var $Ajax_MultiRole;
                    for (var i = 0; i < response.length; i++) {
                        $Ajax_MultiRole = '<label class="col-xs-6 col-md-6"><input name="RoleID" type="checkbox" value="' + response[i].ID + '">' + response[i].RoleName + '</label>'
                        $("#Ajax_MultiRole").append($Ajax_MultiRole)
                    }
                }
            });
        },
        //选中复选框
        Ajax_CheckRole: function () {
            $.L.ajaxHandle({
                url: ACTION_URL("GetRoleUserInfo", "Manager"),
                data: { id: AEditUser_id },
                async: false,
                success: function (response) {
                    if (response) {
                        var RidArr = [];
                        for (var i = 0; i < response.length; i++) {
                            RidArr.push(response[i].Rid)
                        }
                        $("input[name='RoleID']").each(function () {
                            for (var n = 0; n < RidArr.length; n++) {
                                if ($(this).val() == RidArr[n]) {
                                    $(this).attr("checked", "checked")
                                }
                            }
                        })
                    }
                }
            });
        },
        //新增用户
        Ajax_AddUser: function () {
            $.L.ajaxHandle({
                url: ACTION_URL("AddUserInfo", "Manager") + "?rid=" + $("#Rid").val(),
                data: $("#form_AddUser").serialize(),
                success: function (response) {
                    _this.Dialog_AddUserObj.close();
                    if (response) {
                        _this.Ajax_GetUserInfo(1);
                        $.L.msgSuccess("操作成功");
                        $(".btn-primary").attr("disabled", false);
                    }
                }
            });
        },
        //编辑用户
        Ajax_EditUserInfo_: function () {
            $.L.ajaxHandle({
                url: ACTION_URL("EditUserInfo", "Manager"),
                data: $("#form_EditUser").serialize(),
                success: function (response) {
                    _this.Dialog_EditUserObj.close();
                    if (response) {
                        _this.Ajax_GetUserInfo(1);
                        $.L.msgSuccess("操作成功");
                        $(".btn-primary").attr("disabled", false);
                    }
                }
            });
        },
        //给用户绑定角色
        Ajax_EditUserRole: function (id) {

            var RoleValue = "";
            $('input[name="RoleID"]:checked').each(function () {
                RoleValue += $(this).val() + ",";
            });
            $.L.ajaxHandle({
                url: ACTION_URL("EditUserRole", "Manager"),
                data: { id: id, RoleId: RoleValue },
                success: function (response) {
                    if (response ==0) {
                        $.L.msgWarning("请选择用户角色！");
                    }
                    else{
                    _this.Dialog_AEditUserObj.close();
                    _this.Ajax_GetUserInfo(1);
                    $.L.msgSuccess("操作成功");
                    }
                    $(".btn-primary").attr("disabled", false);
                }
            });
        },
        Ajax_DelUser: function (obj) {

            $.L.msgConfirm("确认删除该记录?", function () {
                var id = $(obj).attr("UserID");

                $.L.ajaxHandle({
                    url: ACTION_URL("DelUser", "Manager"),
                    data: { id: id },
                    success: function (response) {
                        if (response > 0) {

                            $.L.msgSuccess("操作成功!");

                            _this.Ajax_GetUserInfo(_index);
                        }
                        else {
                            $.L.msgError("操作失败！");
                        }
                    }
                })
            })

        },
        Ajax_ResetUserPWD: function (id) {

            $.L.ajaxHandle({
                url: ACTION_URL("ResetUserPWD", "Manager"),
                data: { id: id, newPwd: $("#newPwd").val() },
                success: function (response) {
                    if (response > 0) {
                        $.L.msgSuccess("操作成功!");
                        _this.Dialog_ResetCodeObj.close();
                        _this.Ajax_GetUserInfo(1);
                    }
                    else {
                        $.L.msgError("操作失败！");
                    }
                    $(".btn-primary").attr("disabled", false);
                }
            })
        },

    });
})(jQuery);

(function () {
    var _this = $.MGJ.UserManager;
    $.extend($.MGJ.UserManager, {


    });
})(jQuery);
